<?php

namespace App\Providers;

use App\Models\UserTokenModel;
use Illuminate\Cache\RateLimiting\Limit;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\RateLimiter;
use Illuminate\Support\ServiceProvider;
use Laravel\Sanctum\Sanctum;

class AppServiceProvider extends ServiceProvider
{
    /**
     * Register any application services.
     */
    public function register(): void
    {
        //
    }

    /**
     * Bootstrap any application services.
     */
    public function boot(): void
    {
      // 中间件<throttle:api> 速率限制
      RateLimiter::for('api', function (Request $request) {
        return Limit::perMinute(60)->by($request->user()?->id ?: $request->ip())->response(function () {
          return response('error: too many requests', 429);
        });
      });

      // 设置访问令牌模型名称
      Sanctum::usePersonalAccessTokenModel(UserTokenModel::class);
    }
}
